package services;

import dao.*;
import models.*;
import play.db.jpa.JPAApi;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import java.util.List;
import java.util.concurrent.CompletionStage;
import java.util.function.Function;
import java.util.stream.Stream;

import static java.util.concurrent.CompletableFuture.supplyAsync;


/**
 * Provide JPA operations running inside of a thread pool sized to the connection pool
 */
public class DeliveryInfo_Repository_JPA implements DeliveryInfo_Repository {

    private final JPAApi jpaApi;
    private final DatabaseExecutionContext executionContext;

    @Inject
    public DeliveryInfo_Repository_JPA(JPAApi jpaApi, DatabaseExecutionContext executionContext) {
        this.jpaApi = jpaApi;
        this.executionContext = executionContext;
    }

    @SuppressWarnings("unchecked")
    @Override
    public CompletionStage<List<DeliveryInfo_F_Entity>> GetDeliveryInfo(String DeliveryDate, String PhoneNumber, String KHMC, String HWMC, String DeliveryOrderId, String ParentSubId, String SubId)
    {
    	return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
            	// 插入一个原生SQL的测试,调用了SQLServer的自定义函数.
            	String strSql = "select * from dbo.f_GetDeliveryinfo('" + DeliveryDate + "','"
            	                                                        + PhoneNumber + "','"
            			                                                + KHMC +  "','"
            			                                                + HWMC +  "','"
            			                                                + DeliveryOrderId + "','"
            			                                                + ParentSubId + "','"
            			                                                + SubId + "') order by fd_Rowid desc";
            	return entityManager.createNativeQuery(strSql, DeliveryInfo_F_Entity.class).getResultList();
            	

            	
            });
        }, executionContext);    	
    }
     
    @SuppressWarnings("unchecked")
	@Override
    public CompletionStage<List<DeliveryInfo_T_Entity>> CreatSubBill(String PhoneNumber,String DeliveryOrderid, String SubId, String DeliveryQty, String StartDate, String EndDate , String ExecPosi , String ExecBy)
    {
    	return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
            	// 插入一个原生SQL的测试,调用了SQLServer的自定义函数.
            	String strSql = "exec p_CreateSubBill @PhoneNumber = '"+PhoneNumber+"',@DeliveryOrderid = '"
            																+ DeliveryOrderid + "', @SubId = '"
            	                                                        	+ SubId + "', @DeliveryQty ="
            	                                                        	+ DeliveryQty +  ", @StartDate ='"
            	                                                        	+ StartDate +  "', @EndDate ='"
            	                                                        	+ EndDate + "', @ExecPosi ='"
            	                                                        	+ ExecPosi + "', @ExecBy ='"
            	                                                        	+ ExecBy + "' ";
//            	return entityManager.createNativeQuery(strSql, DeliveryInfo_T_Entity.class).getResultList();

				List<DeliveryInfo_T_Entity> list =
						entityManager.createNativeQuery(strSql, DeliveryInfo_T_Entity.class).getResultList();
            	if(list.size() !=0) {
            		return list;
            	}else {
            		String strSql2 = "select * from f_GetLastErr('" + PhoneNumber + "') ";
            		return entityManager.createNativeQuery(strSql2, ErrorLog_T_Entity.class).getResultList();
            	}
            });
        }, executionContext);    	
    }
}
